const RouterHook = require("../module/routerModule")

const {
  sendCode
} = require("../service/common")
const {
  login,
  smsLogin,
  register,
  repass
} = require("../service/user")
const {
  StringUtils,
  AssertUtils
} = require("../util/utils")
const router = RouterHook()

/**
 * @swagger
 * /api/common/login:
 *    post:
 *      tags:
 *      - 通用接口
 *      summary: 账号密码登录
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: username
 *          description: 用户名
 *          required: true
 *          type: string
 *        - name: password
 *          description: 密码
 *          required: true
 *          type: string
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              required:
 *                - username
 *                - password
 *              properties:
 *                username:
 *                  type: string
 *                  default: test
 *                password:
 *                  type: string
 *                  default: 123456
 *      responses:
 *        200:
 *          description: 返回Token
 *          schema:
 *            type: object
 *            properties:
 *              token:
 *                type: string
 *                default: xxxxxxxxxxxxxxxxxxx
 *        500:
 *          description: 账号密码错误
 * */
router.post('/login', async (ctx, next) => {
  const {
    username,
    password
  } = ctx.request.body
  AssertUtils.StringNotEmpty(username, '用户名不能为空')
  AssertUtils.StringNotEmpty(password, '密码不能为空')
  let token = await login(username, password)
  ctx.result.success({
    token
  })
}).noAuth()
/**
 * @swagger
 * /api/common/send_code:
 *    post:
 *      tags:
 *      - 通用接口
 *      summary: 发送验证码
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: phone
 *          description: 手机号
 *          required: true
 *          type: string
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              required:
 *                - phone
 *              properties:
 *                phone:
 *                  type: string
 *                  default: 138000000
 *                  description: 手机
 *      responses:
 *        200:
 *          description: 返回验证码
 *          schema:
 *            type: object
 *            properties:
 *              code:
 *                type: string
 *                default: 111111
 *        500:
 *          description: 手机号不能为空
 * */
router.post('/send_code', async (ctx, next) => {
  const {
    phone
  } = ctx.request.body
  AssertUtils.StringNotEmpty(phone, '手机号不能为空')
  let code = await sendCode(phone)
  ctx.result.success({
    code
  })
}).noAuth()

/**
 * @swagger
 * /api/common/sms_login:
 *    post:
 *      tags:
 *      - 通用接口
 *      summary: 验证码登录
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: phone
 *          description: 手机号
 *          required: true
 *          type: string
 *        - name: code
 *          description: 验证码
 *          required: true
 *          type: string
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              required:
 *                - phone
 *                - code
 *              properties:
 *                phone:
 *                  type: string
 *                  default: 138000000
 *                  description: 手机
 *                code:
 *                  type: string
 *                  default: asdasd
 *                  description: 验证码
 *      responses:
 *        200:
 *          description: 返回Token
 *          schema:
 *            type: object
 *            properties:
 *              token:
 *                type: string
 *                default: xxxxxxxxxxxxxxxxxxx
 *        500:
 *          description: 验证码错误
 * */
router.post('/sms_login', async (ctx, next) => {
  const {
    phone,
    code
  } = ctx.request.body
  AssertUtils.StringNotEmpty(phone, '手机号不能为空')
  AssertUtils.StringNotEmpty(code, '验证码不能为空')
  let token = await smsLogin(phone, code)
  ctx.result.success({
    token
  })
}).noAuth()
/**
 * @swagger
 * /api/common/repass:
 *    post:
 *      tags:
 *      - 通用接口
 *      summary: 找回密码
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: phone
 *          description: 手机
 *          required: true
 *          type: string
 *        - name: password
 *          description: 密码
 *          required: true
 *          type: string
 *        - name: code
 *          description: 验证码
 *          required: true
 *          type: string
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              required:
 *                - phone
 *                - code
 *                - password
 *              properties:
 *                phone:
 *                  type: string
 *                  default: 138000000
 *                  description: 手机
 *                code:
 *                  type: string
 *                  default: asdasd
 *                  description: 验证码
 *                password:
 *                  type: string
 *                  default: asdasd
 *                  description: 密码
 *      responses:
 *        200:
 *          description: 返回Token
 *          schema:
 *            type: object
 *            properties:
 *              token:
 *                type: string
 *                default: xxxxxxxxxxxxxxxxxxx
 *        500:
 *          description: 验证码错误
 * */
router.post('/repass', async (ctx, next) => {
  const {
    phone,
    password,
    code
  } = ctx.request.body
  AssertUtils.StringNotEmpty(phone, '手机号不能为空')
  AssertUtils.StringNotEmpty(password, '密码不能为空')
  AssertUtils.StringNotEmpty(code, '验证码不能为空')
  await repass(phone, code, password)
  ctx.result.success({}, "修改成功")
}).noAuth()
/**
 * @swagger
 * /api/common/reg:
 *    post:
 *      tags:
 *      - 通用接口
 *      summary: 注册用户
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: username
 *          description: 用户名
 *          required: true
 *          type: string
 *        - name: password
 *          description: 密码
 *          required: true
 *          type: string
 *        - name: name
 *          description: 昵称
 *          required: true
 *          type: string
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              required:
 *                - username
 *                - password
 *                - name
 *              properties:
 *                username:
 *                  type: string
 *                  default: test
 *                password:
 *                  type: string
 *                  default: asdasdqwd
 *                name:
 *                  type: string
 *                  default: 小黑
 *      responses:
 *        200:
 *          description: 返回Token
 *          schema:
 *            type: object
 *            properties:
 *              token:
 *                type: string
 *                default: xxxxxxxxxxxxxxxxxxx
 *        500:
 *          description: 用户名不能为空
 * */
router.post('/reg', async (ctx, next) => {
  const {
    username,
    password,
    phone,
    name
  } = ctx.request.body
  AssertUtils.StringNotEmpty(username, '用户名不能为空')
  AssertUtils.StringNotEmpty(password, '密码不能为空')
  AssertUtils.StringNotEmpty(name, '名称不能为空')
  AssertUtils.StringNotEmpty(phone, '手机不能为空')
  let regUser = await register(username, password, phone, name)
  ctx.result.success({}, "注册成功")
}).noAuth()

router.post('/upload', async (ctx, next) => {
  AssertUtils.ObjectNotEmpty(ctx.request.files, "请上传文件")
  const {
    file
  } = ctx.request.files
  ctx.result.success({
    originalFilename: file.originalFilename,
    newFilename: file.newFilename,
    size: file.size,
    path: '/resource/uploads/' + file.newFilename
  }, "上传成功")
}).noAuth()

module.exports = router